import { Request } from "express";
import { dataSource } from "@dataSource";
import { Repository, SelectQueryBuilder, UpdateResult, ObjectLiteral } from "typeorm";
import Page from "@model/Page"
/**
 * 基本服务
 */
export abstract class BaseService<T extends ObjectLiteral> {

  private readonly repository: Repository<T>;
  constructor(entity: any) {
    this.repository = dataSource.getRepository(entity);
  }


  async get(id: string): Promise<T | null> {
    return await this.repository.createQueryBuilder().where({ id }).getOne();
  }

  async getAccount(account: string): Promise<T | null> {
    return await this.repository.createQueryBuilder().where({ account }).getOne();
  }

  async save(entity: T, request: Request): Promise<T> {

    return await this.repository.save(entity);
  }
  async update(entity: T, request: Request): Promise<T> {


    return await this.repository.save(entity);
  }

  /**
   * 
   * @param entity id集
   * @returns 
   */
  async delete(entity: { ids: [] }): Promise<UpdateResult> {
    const { ids, ...instance } = entity;
    return await this.repository.update(ids, instance as object);
  }
}

